﻿using System;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI;
using System.Globalization;
using System.Text;
using System.Collections.Generic;
using SiteUnity.Layers.Entity.PageItems.Files;
using SiteUnity.Layers.Application;
using SiteUnity.Frame.Common.Tools.Class;
using SiteUnity.Layers.Application.Files;
using SiteUnity.Frame.Modules.Gallery.Slider;

namespace SiteUnity.Frame.Modules
{
    public enum SliderType
    {
        PageImages,
        LastPages
    }

    [DefaultProperty("Text")]
    [ToolboxData("<{0}:Slider runat=server />")]
    public class Slider : WebControl
    {
        public List<SliderItem> SliderItems { get; set; }
        public FileType ImageType { get; set; }
        public SliderType Type { get; set; }
        public int ItemCount { get; set; }
        public string SliderID { get; set; }
        
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (ImageType == default(FileType))
                ImageType = FileType.img_960x360;

            if (SliderItems == null)
                switch (Type)
                {
                    case SliderType.PageImages:
                        SliderItems = prepareImages();
                        break;
                    case SliderType.LastPages:
                        SliderItems = prepareLastPages();
                        break;
                }
        }

        private List<SliderItem> prepareLastPages()
        {
            List<SiteUnity.Layers.Entity.General.Page> pages = SiteManager.selectRecentPage(ItemCount*3);
            List<SliderItem> sliderItem = new List<SliderItem>();

            int i = 0;
            foreach (SiteUnity.Layers.Entity.General.Page page in pages)
            {
                List<File> pageImages = ClassCollection.FindAll(FileManager.getFileByPage(page), "PageGuid", page.Guid);
                
                foreach (File image in pageImages)
                {
                    if (image.TypeID == ImageType)
                    {
                        if (ItemCount != 0 && i == ItemCount)
                            break;

                        SliderItem newItem = new SliderItem(image, page.Url);
                        newItem.Title = page.Title;
                        sliderItem.Add(newItem);

                        i++;

                        break;
                    }
                }
            }
            return sliderItem;
        }

        private List<SliderItem> prepareImages()
        {
            SiteUnity.Layers.Entity.General.Page page = SiteManager.Website.CurrentPage;
            List<File> pageImages = ClassCollection.FindAll(FileManager.getFileByPage(page), "PageGuid", page.Guid);
            List<SliderItem> sliderItem = new List<SliderItem>();

            int i = 0;
            foreach (File image in pageImages)
            {
                if (image.TypeID == ImageType)
                {
                    if (ItemCount != 0 && i == ItemCount)
                        break;

                    SliderItem newItem = new SliderItem(image);
                    sliderItem.Add(newItem);

                    i++;
                }
            }

            return sliderItem;
        }

        private void GenerateSlider(HtmlTextWriter output)
        {
            if (SliderItems != null && SliderItems.Count > 0)
            {
                output.AddAttribute(HtmlTextWriterAttribute.Id, SliderID);
                output.RenderBeginTag(HtmlTextWriterTag.Div);

                foreach (SliderItem image in SliderItems)
                {
                    if (string.IsNullOrEmpty(image.PageUrl))
                        output.AddAttribute(HtmlTextWriterAttribute.Href, "#");
                    else
                        output.AddAttribute(HtmlTextWriterAttribute.Href, image.PageUrl);

                    output.RenderBeginTag(HtmlTextWriterTag.A);

                    if (!string.IsNullOrEmpty(image.Title))
                    {
                        output.RenderBeginTag(HtmlTextWriterTag.Span);
                        output.Write(image.Title);
                        output.RenderEndTag();
                    }

                    output.AddAttribute(HtmlTextWriterAttribute.Src, image.Url);
                    output.AddAttribute(HtmlTextWriterAttribute.Alt, "");
                    output.AddAttribute(HtmlTextWriterAttribute.Title, "");

                    output.RenderBeginTag(HtmlTextWriterTag.Img);
                    output.RenderEndTag();

                    output.RenderEndTag();
                }

                output.RenderEndTag();
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (SliderItems != null && SliderItems.Count > 0)
            {
                if (SliderItems.Count == 1)
                    Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "readyNivo", string.Format("var pauseTime = {0}; var SliderID={1}", "9999999", SliderID), true);
                else
                    Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "readyNivo", string.Format("var pauseTime = {0}; var SliderID={1}", "4000", SliderID), true);
            }
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            StringBuilder sb = new StringBuilder();
            HtmlTextWriter htw = new HtmlTextWriter(new System.IO.StringWriter(sb, CultureInfo.InvariantCulture));

            GenerateSlider(output);

            output.Write(sb.ToString());
        }
    }
}
